package pe.edu.upc.dsd.ws;


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import pe.edu.upc.dsd.ws.AntecedentesPolicialesBean;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import pe.edu.upc.dsd.ws.AntecedentesPolicialesService;


public class ServletServicios extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
           
        } finally { 
            out.close();
        }
    } 

   
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
      

    } 

    
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

        String nombre = request.getParameter("nombre");
        String apellido = request.getParameter("apellido");
        String mensaje="";
        final Logger logger = Logger.getLogger(ServletServicios.class);
        
        AntecedentesPolicialesService antecedentes;
        RequestResponseClient requestResponseClient;
        ApplicationContext context = new ClassPathXmlApplicationContext("/applicationContext.xml");

        antecedentes= context.getBean("antecedentesClient",AntecedentesPolicialesService.class);
        requestResponseClient=context.getBean("requestResponseClient",RequestResponseClient.class);
		
        List<AntecedentesPolicialesBean> antecedentesPoliciales = antecedentes.getListaAntecedentes(nombre,apellido);
        String mensajeResp="Enviando y recibiendo nuevo curriculum";
        requestResponseClient.sendAndReceive(mensajeResp);
        logger.debug("Consumiendo mensaje de cola");
        
        if (antecedentesPoliciales==null){
        	mensaje="No presenta antecedentes Policiales";
        	logger.debug("Nro de Antecedentes: " + mensaje);
        } else
        	logger.debug("Nro de Antecedentes: " + antecedentesPoliciales.size());
        
        request.setAttribute("antecedentes", antecedentesPoliciales);
        request.setAttribute("mensaje", mensaje);       
        
        request.getRequestDispatcher("index.jsp").forward(request, response);

    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
